# -*- coding: utf-8 -*-

import scrapy
from chinahr.items import JobInfoItem, ComInfoItem
from scrapy.loader import ItemLoader
from scrapyluke.processors import *
import json
import logging

__author__ = 'yanyan'


class JobcnSpider(scrapy.Spider):
    name = 'jobcn'
    allowed_domains = ['jobcn.com']
    start_urls_from = 'db'

    def parse(self, response):
        data = json.loads(response.body_as_unicode())
        total_page = data['pageCount']

        if total_page == '25':
            logging.log(level='INFO', msg='Page %s is not fully presented.\n' % response.url)

        for page in range(1, int(total_page)):
            yield scrapy.Request('%s&p.pageNo=%s' % (response.url, int(page)), callback=self.parse_urls,
                                 headers={'Content-Type': 'application/json; charset=UTF-8'})

    def parse_urls(self, response):
        data = json.loads(response.body_as_unicode())
        for row in data['rows']:
            job_url = 'http://www.jobcn.com/position/detail.xhtml?redirect=0&posId=%s&comId=%s&s=search/advanced&acType=1' % (row['posId'], row['comId'])
            com_url = 'http://www.jobcn.com/position/company.xhtml?comId=%s' % row['comId']
            yield scrapy.Request(job_url, callback=self.parse_job_info)
            yield scrapy.Request(com_url, callback=self.parse_com_info)

    def parse_job_info(self, response):
        loader = ItemLoader(item=JobInfoItem(), response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('job_name', u'//h2[@class="name hlkw"]/text()')
        loader.add_xpath('job_company', u'//div[@class="base"]/h2/text()', ReplaceBlank(''), TakeFirstL())
        loader.add_xpath('job_location', u'//div[@class="name_pos"]/div[@class="attributes clearfix"]/dl[last()]/dd/text()', ReplaceBlank(''))
        loader.add_xpath('job_salary', u'//div[@class="salary_body"]/text()', ReplaceBlank(''))
        loader.add_xpath('job_miniEdu', u'//div[@class="info_pos"]/div[@class="attributes clearfix"]/dl/dt[text()="学历要求"]/../dd/text()')
        loader.add_xpath('job_experience', u'//div[@class="info_pos"]/div[@class="attributes clearfix"]/dl/dt[text()="工作经验"]/../dd/text()')
        loader.add_xpath('job_reqAge', u'//div[@class="info_pos"]/div[@class="attributes clearfix"]/dl/dt[text()="年龄要求"]/../dd/text()')
        loader.add_xpath('job_reqSex', u'//div[@class="info_pos"]/div[@class="attributes clearfix"]/dl/dt[text()="性别要求"]/../dd/text()')
        loader.add_xpath('job_desc', u'//div[@class="desc_pos hlkw"]/text()', RemoveTagsL(), ReplaceBlank(''), JoinL('\n'))
        loader.add_xpath('job_desc_loc', u'//div[@class="tab_map_title"]/text()', TakeFirstL())
        loader.add_xpath('job_benefits', u'//div[@class="weal_pos"]/div[@class="weal_body"]/ul[@class="clearfix"]/li/a/span[@class="hlkw"]/text()', JoinL())
        loader.add_xpath('job_experience', u'//div[@class="info_pos"]/div[@class="attributes clearfix"]/dl/dt[text()="工作经验"]/../dd/text()')
        return loader.load_item()

    def parse_com_info(self, response):
        loader = ItemLoader(item=ComInfoItem(), response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('com_name', u'//div[@class="base"]/h2/text()', ReplaceBlank(''), TakeFirstL())
        loader.add_xpath('com_intro', u'//div[@class="item company"]/div[@class="item_body"]/text()', RemoveTagsL(), ReplaceBlank(''), JoinL('\n'))
        loader.add_xpath('com_industry', u'//div[@class="item business_card"]/div[@class="item_body"]/dl/dt[text()="行业："]/../dd/a/text()')
        loader.add_xpath('com_size', u'//div[@class="item business_card"]/div[@class="item_body"]/dl/dt[text()="规模："]/../dd/text()')
        loader.add_xpath('com_link', u'//div[@class="item business_card"]/div[@class="item_body"]/dl/dt[text()="主页："]/../dd/a/text()')
        loader.add_xpath('com_address', u'//div[@class="item business_card"]/div[@class="item_body"]/dl/dt[text()="地址："]/../dd/text()')
        return loader.load_item()

